package com.rc.saas.mini.controller.enterprise;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import com.rc.saas.mini.model.enterprise.EnterpriseMember;
import com.rc.saas.mini.model.rc.RcMemberFlag;
import com.rc.saas.mini.service.enterprise.EnterpriseMemberService;
import com.rc.saas.mini.service.rc.RcMemberFlagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Author
 * @Date: 2022-06-02
 */

@Api(tags = "23.企业信息")
@Controller
@RequestMapping("/enterpriseMember")
public class EnterpriseMemberController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(EnterpriseMemberController.class);

    @Autowired
    private EnterpriseMemberService enterpriseMemberService;
    @Autowired
    private RcMemberFlagService rcMemberFlagService;


    /**
     * 查询
     */
    @ApiOperation(value = "查询用户自己企业信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectByMyself", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<EnterpriseMember> selectByMyself(HttpServletRequest request, Long enterpriseId) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (loginInfo == null || loginInfo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        RcMemberFlag rcMemberFlag=rcMemberFlagService.selectByPrimaryKey(loginInfo.getMemberId());
        if(rcMemberFlag==null||rcMemberFlag.getEnterpriseMemberId()==null){
            return DataTResponse.SUCCESS("用户无关联企业信息");
        }
        return DataTResponse.SUCCESS_DATA(enterpriseMemberService.selectByPrimaryKey(rcMemberFlag.getEnterpriseMemberId()));
    }
    /**
     * 查询
     */
    @ApiOperation(value = "查询单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectByEnterpriseId", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", paramType = "query", value = "id", required = true),
    })
    private DataTResponse<EnterpriseMember> selectByEnterpriseId(Long enterpriseId) {
        return DataTResponse.SUCCESS_DATA(enterpriseMemberService.selectByPrimaryKey(enterpriseId));
    }

    /**
     * 查询列表
     */
    @ApiOperation(value = "查询列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/listByEnterpriseMember", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseName", paramType = "query", value = "企业名字模糊匹配", required = true),
    })
    private DataTResponse<List<EnterpriseMember>> listByEnterpriseMember(EnterpriseMember enterpriseMember) {
        List<EnterpriseMember> list = enterpriseMemberService.listByCondition(enterpriseMember);
        return DataTResponse.SUCCESS_DATA(list);
    }
}
